import os
from datetime import timedelta
from pathlib import Path


# 获取项目基目录
basedir = os.path.abspath(os.path.dirname(__file__))


# 基础配置
class Config:
    SECRET_KEY = '2-4V/QWe9tn4'
    UPLOAD_FOLDER = 'static/images'
    DATA_DIR = Path("data")
    STATS_FILE = DATA_DIR / 'stats.json'
    USER_FILE = DATA_DIR / 'users.json'
    MAX_DAILY_ADDITIONS = 5  # 普通用户每日最大添加数量

    # 记住我功能的cookie过期时间（默认为365天）
    REMEMBER_COOKIE_DURATION = timedelta(days=30)

    # 邮件服务器配置 (QQ邮箱)
    MAIL_SERVER = 'smtp.qq.com'
    MAIL_PORT = 465
    MAIL_USERNAME = '1544717589@qq.com'
    MAIL_PASSWORD = 'ziubddyrtavpbabd'  # QQ邮箱授权码
    MAIL_USE_TLS = False    # QQ邮箱使用SSL，而非TLS
    MAIL_USE_SSL = True      # 必须启用SSL
    MAIL_DEFAULT_SENDER = ('徒步社区', '1544717589@qq.com')

    # 图片文件上传配置
    IMAGES_UPLOAD_FOLDER = os.path.join(basedir, 'static', 'uploads')
    ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg', 'gif'}
    MAX_CONTENT_LENGTH = 16 * 1024 * 1024  # 16MB

    # 确保目录存在
    os.makedirs(UPLOAD_FOLDER, exist_ok=True)
    DATA_DIR.mkdir(exist_ok=True)

    # 日志配置
    LOGGING_CONFIG = {
        'version': 1,
        'disable_existing_loggers': False,
        'formatters': {
            'standard': {
                'format': '%(asctime)s [%(levelname)s] %(name)s: %(message)s'
            },
        },
        'handlers': {
            'console': {
                'class': 'logging.StreamHandler',
                'formatter': 'standard',
                'level': 'DEBUG'
            },
            'file': {
                'class': 'logging.handlers.RotatingFileHandler',
                'filename': 'app.log',
                'maxBytes': 10485760,  # 10MB
                'backupCount': 5,
                'formatter': 'standard',
                'level': 'INFO'
            },
        },
        'loggers': {
            '': {  # root logger
                'handlers': ['console', 'file'],
                'level': 'DEBUG',
                'propagate': True
            },
        }
    }